//
//  BaseNavigationController.m
//  微blog
//
//  Created by Jorn.Wu on 15/8/19.
//  Copyright (c) 2015年 Jorn.Wu(jorn_wza@sina.com). All rights reserved.
//

#import "BaseNavigationController.h"
#import "ThemeManager.h"
#import "RootTabBarViewController.h"

@interface BaseNavigationController ()<UINavigationControllerDelegate>

@end

@implementation BaseNavigationController

/**
 * 重写dealloc方法，旨在移除观察者
 */
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kThemeDidChangedNotification object:nil];
}


/**
 * 重写initWithCoder方法，旨在让导航栏可以响应切换主题通知(注意：在本工程中，navigation是从sb中加载过来的，
 * 所以应重写的是该方法，而不是"- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil")
 */
- (id)initWithCoder:(NSCoder *)aDecoder{
    
    if (self = [super initWithCoder:aDecoder]) {

        /**
         * 设置导航栏为不透明样式
         */
        self.navigationBar.translucent = NO;
                
        /**
         * 注册成为接收者
         */
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(themeDidChangedNotificationAction:) name:kThemeDidChangedNotification object:nil];
    }
    
    return self;
    
}


/**
 * 重写awakeFromNib方法，旨在方便XIB创建
 */
- (void)awakeFromNib{
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(themeDidChangedNotificationAction:) name:kThemeDidChangedNotification object:nil];
    
}


/**
 * 通知的响应方法
 */
- (void)themeDidChangedNotificationAction:(NSNotification *)noti{
    [self reloadIamge];
    [self reloadTitleColor];
}


//--------------------------------------设置背景图片-----------------------------------------------------

/**
 * 重新加载图片
 */
- (void)reloadIamge{
    ThemeManager *manager = [ThemeManager defualtManager];
    
    /**
     * 通过主题管理对象manager来获取图
     */
    UIImage *image = [manager getThemeImage:self.backgroudImageName];
    
    /**
     * 设置图片
     */
    if (image) {
        [self.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
    }
    
}


/**
 * 重新set方法，旨在更改图片
 */
- (void)setBackgroudImageName:(NSString *)backgroudImageName{
   
    if (![_backgroudImageName isEqualToString:backgroudImageName]) {
        _backgroudImageName = [backgroudImageName copy];
        [self reloadIamge];
    }
    
}

//--------------------------------------设置title颜色-----------------------------------------------------

/**
 * 重新加载title颜色
 */
- (void)reloadTitleColor{
    ThemeManager *manager = [ThemeManager defualtManager];
    
    /**
     * 通过主题管理对象manager来获取title颜色
     */
    UIColor *color = [manager getThemeColor:self.titlColorName];

    /**
     * 设置图片
     */
    if (color) {
        
        NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
        [attributes setObject:color forKey:NSForegroundColorAttributeName];
        self.navigationBar.titleTextAttributes = attributes;
        
        self.navigationBar.tintColor = color;       //同时设置item的颜色
        
    }
    
}


/**
 * 重写set方法，旨在更改title颜色
 */
- (void)setTitlColorName:(NSString *)titlColorName{
    
    if (![_titlColorName isEqualToString:titlColorName]) {
        _titlColorName = [titlColorName copy];
        [self reloadTitleColor];
    }
    
}


- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
    if (navigationController.viewControllers.count > 1) {
        RootTabBarViewController *rooVC = (RootTabBarViewController *)navigationController.parentViewController;
        rooVC.tabBar.hidden = YES;
    }else {
        RootTabBarViewController *rooVC = (RootTabBarViewController *)navigationController.parentViewController;
        rooVC.tabBar.hidden = NO;
    }
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.delegate = self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
